package com.chainyoung.trademarket.push;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.chainyoung.common.utils.AppUtils;
import com.chainyoung.component_tmtrade.constant.StringConstant;
import com.chainyoung.component_tmtrade.ui.activity.TradeFiatCurrencyOrderDetailActivity;
import com.chainyoung.router.RouterConstants;
import com.chainyoung.router.RouterUtils;
import com.chainyoung.trademarket.constant.NotificationChannels;
import com.chainyoung.trademarket.entity.PushMessage;
import com.chainyoung.trademarket.ui.MainMarketActivity;
import com.chainyoung.trademarket.ui.MessageDetailActivity;
import com.chainyoung.trademarket.update.UpdateService;
import com.chainyoung.trademarket.utils.NotificationUtils;
import com.google.gson.Gson;

import cn.jpush.android.api.JPushInterface;

import static com.chainyoung.common.constant.CommonStringConstants.URL;

public class PushReviever extends BroadcastReceiver {


    private static final String LEGAL_ORDER = "legal_order";
    private static final String SYSTEM_MSG = "system_msg";
    private static final String APP_UPDATE = "app_update";

    @Override
    public void onReceive(Context context, Intent intent) {

        Bundle bundle = intent.getExtras();
        if (bundle != null && intent.getAction() != null) {
            switch (intent.getAction()) {
                case JPushInterface.ACTION_MESSAGE_RECEIVED:
                    //自定义消息
                    showNotifyWindow(context, bundle);
                    break;
                case JPushInterface.ACTION_NOTIFICATION_RECEIVED:
                    //接收通知
                    break;
                case JPushInterface.ACTION_NOTIFICATION_OPENED:
                    //点击打开通知
                    PushMessage pushMessage = null;
                    try {
                        pushMessage = new Gson().fromJson(bundle.getString(JPushInterface.EXTRA_EXTRA), PushMessage.class);
                    } catch (Exception e) {
                    }
                    if (pushMessage != null) {
                        if (pushMessage.getPush_msg_type().equals(LEGAL_ORDER)) {
                            RouterUtils.getInstance()
                                    .build(RouterConstants.PATH_ORDER_DETAIl)
                                    .withString(StringConstant.ID, pushMessage.getOrder_id())
                                    .navigation();
                        } else if (pushMessage.getPush_msg_type().equals(SYSTEM_MSG)) {
                            Intent intent1 = new Intent(context, MessageDetailActivity.class);
                            intent1.putExtra(URL, pushMessage.getContent());
                            intent1.putExtra("title", pushMessage.getTitle());
                            context.startActivity(intent1);
                        }

                    }

                    break;
            }
        }
    }


    private void showNotifyWindow(Context mContext, Bundle bundle) {
        //点击打开通知
        PushMessage pushMessage = null;
        Intent intent = null;
        try {
            pushMessage = new Gson().fromJson(bundle.getString(JPushInterface.EXTRA_EXTRA), PushMessage.class);
        } catch (Exception e) {
        }
        if (pushMessage != null && pushMessage.getPush_msg_type().equals(APP_UPDATE)) {
            if (Integer.valueOf(pushMessage.getVersion_code())>AppUtils.getAppVersionCode(mContext)) {
                intent = new Intent(mContext, UpdateService.class);
                //下载地址
                intent.putExtra("apkUrl", pushMessage.getDownloadurl());
                intent.putExtra("md5", pushMessage.getMd5());
            }
            PendingIntent pendingIntent = PendingIntent.getService(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            String title = bundle.getString(JPushInterface.EXTRA_TITLE);
            String content = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            NotificationUtils.getInstance().sendNotification(mContext, TextUtils.isEmpty(title)?"Cbob":title, content, pendingIntent, 1, NotificationChannels.UPLOAD_APK, true);
        }

    }
}
